package com.plane.ticketse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plane.ticketse.entity.Role;
import com.plane.ticketse.service.IRoleService;
import com.plane.ticketse.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.plane.ticketse.service.IUserService;
import com.plane.ticketse.entity.User;

import org.springframework.web.bind.annotation.RestController;

/**
 * UserController 前端控制器
 * @author 作者
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @Resource
    private IRoleService roleService;
    /**
    *  新增用户
    */
    @PostMapping("/edit")
    public Result save(@RequestBody User user) {
        // 新增用户时判断account是否重复
        if (user.getId()==null){
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("account",user.getAccount());
            User one = userService.getOne(queryWrapper);
            if (one!=null){
                return Result.error("account已存在");
            }
        }
        // 获取前端传过来的roleId
        Role userRole = roleService.getById(user.getUserRole());
        user.setUserRole(userRole.getRoleNo());
        return Result.success(userService.saveOrUpdate(user));
    }
    /**
     * 修改个人信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        User one = userService.getOne(queryWrapper);
        if (one!=null){
            return Result.error("account已存在");
        }
        return Result.success(userService.saveOrUpdate(user));
    }
    /**
    *  删除用户
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }

    /**
     * 登录
     * @param account
     * @param userPwd
     * @return
     */
    @GetMapping("/login")
    public Result login(@RequestParam String account, @RequestParam String userPwd){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("user_pwd",userPwd);
        User theUser = userService.getOne(queryWrapper);
        if (theUser!=null){
            return Result.success(theUser);
        }else {
            return Result.error("账号密码不正确");
        }
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> list=  userService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }
    /**
     * 根据id重置密码
     */
    @GetMapping("/resetPwd/{id}")
    public Result resetPwd(@PathVariable Integer id){
        User theUser = userService.getById(id);
        theUser.setUserPwd("123456");
        if (userService.saveOrUpdate(theUser)){
            return Result.success(true);
        }
        return Result.error("失败");
    }
}

